package com.imy.common.manger;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;



import java.util.ArrayList;
import java.util.List;

public class TaskManger {

    private static volatile TaskManger get = new TaskManger();

    private List<Activity> task = new ArrayList<>();

    public static TaskManger get() {
        return get;
    }

    public Activity getTopTask() {
        return task.get(task.size() - 1);
    }

    public void init(Application application) {
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                task.add(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                task.remove(activity);
            }
        });
    }
}
